import org.jsecurity.SecurityUtils
class HomeController {
    def securityService
    def postService

    def index = {
        log.info("第一个页面")
        def user = securityService.getCurrentUser()
        if(user){
            return redirect(action:"blog",id:user.id)
        }
        // postService.getDisplayablePosts(params.max, params.offset)
        redirect(action:"login",controller:"auth")
    }

    

    def space = {
        def author = Teacher.findByHostId(params.id)
        if(!author){
            flash.message = "没有找到您的空间，请联系管理员！"
            return render(view: "../errors/siteStop")
        }
        redirect(action:"blog",id:author.id)

    }

    def blog = {
        def globalID = params.id
        
        def user = securityService.getCurrentUser()
        if(!globalID && user){
            if(user in Parent){
                log.info("ParentParentParent")
                globalID = user.student.id
            }else{
                globalID = user.id
            }
        }
//        if(!globalID){
//            return redirect(action:"index")
//        }
        
        Author author = Author.get(globalID)

        if(SecurityUtils.subject.hasRole("SUPER_ADMIN")){
              return render(view: "index", model: postService.getPersonDisplayablePosts(user,author,params.max, params.offset))
        }

        if(user.school.id != author.school.id){
            flash.message = "你访问的空间和你不属于同一学校，无权访问！"
            return render(view: "../errors/siteStop")
        }

        if(author.site?.enabled){
            return render(view: "index", model: postService.getPersonDisplayablePosts(user,author,params.max, params.offset))
        }else{
            flash.message = "此站点已经被管理员关闭"
            return render(view: "../errors/siteStop")
        }
    }
}
